package com.atgion.manager.controller;

import com.atgion.manager.service.SysRoleService;
import com.atgion.common.log.annoyation.Log;
import com.atgion.spzx.model.dto.system.SysRolePermissionUpdateDTO;
import com.atgion.spzx.model.entity.system.RoleParam;
import com.atgion.spzx.model.entity.system.SysRole;
import com.atgion.spzx.model.vo.base.Result;
import com.atgion.spzx.model.vo.system.SysRolePermissionAddDTO;
import com.atgion.spzx.model.vo.system.SysRoleVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
@Tag(name = "SysRoleController", description = "用户角色控制层")
//@RequestMapping("/sys/role")
@RequestMapping("/systemManage")
public class SysRoleController {

    private final SysRoleService sysRoleService;

    @Operation(summary = "list 分页列表")
    @Parameters({
            @Parameter(name = "current", description = "当前页", required = true, example = "1"),
            @Parameter(name = "size", description = "每页显示条数", required = true, example = "10"),
            @Parameter(name = "username", description = "用户名称"),
    })
    @GetMapping(value = "/getRoleList")
    public Result<IPage<SysRole>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        IPage<SysRole> sysUsers = sysRoleService.getPage(params);
        return Result.success(sysUsers);
    }

    @Operation(summary = "查询角色信息")
    @GetMapping(value = "/getAllRoles")
    public Result<List<SysRoleVO>> getAllRoles(@RequestHeader("Authorization") String authorizationHeader) {
        return sysRoleService.getAllRoles(authorizationHeader);
    }

    @Operation(summary = "新增角色信息")
    @PostMapping(value = "/addRole")
    public Result<Boolean> add(@RequestBody SysRole sysRole) {
        return sysRoleService.add(sysRole);
    }

    @Log(title = "修改角色权限", businessType = 2)
    @PostMapping
    @Operation(operationId = "3", summary = "根据角色 ID 保存权限")
    public Result<Boolean> add(@Parameter(description = "新增对象") @RequestBody SysRolePermissionAddDTO sysRolePermissionAddDTO) {
        boolean added = sysRoleService.addPermissionForRoleId(sysRolePermissionAddDTO.getRoleId(), sysRolePermissionAddDTO.getPermissionIds());
        return Result.success(added);
    }

    @PutMapping
//    @SaCheckPermission("sys:role:permission:update")
    @Operation(operationId = "4", summary = "更新角色权限管理信息")
    public Result<Boolean> update(@Parameter(description = "更新对象") @RequestBody SysRolePermissionUpdateDTO sysRolePermissionUpdateDTO) {
        return Result.success(sysRoleService.updatePermission(sysRolePermissionUpdateDTO));
    }

    @Operation(summary = "删除角色信息")
    @DeleteMapping(value = "/remove/{id}")
    public Result<Boolean> remove(@PathVariable("id") Integer id) {
         sysRoleService.removeById(id);
         return Result.success();
    }

    @Operation(summary = "查询角色资源ID")
    @PostMapping(value = "/getRoleResourceId")
    public Result<List<Long>> getRoleResourceId(@RequestParam Long roleId) {
        return sysRoleService.getRoleResourceId(roleId);
    }

    @Operation(summary = "修改角色资源信息")
    @PostMapping(value = "/updateRoleResourceInfo")
    public Result<Boolean> updateRoleResourceInfo(@RequestBody RoleParam roleParam) {
        return sysRoleService.updateRoleResourceInfo(roleParam.getRoleId(), roleParam.getResourceId());
    }


    @GetMapping("/permission/{roleId}")
//    @SaCheckPermission("sys:role:permission:queryPermsWithRoleId")
    @Operation(operationId = "7", summary = "根据角色 ID 获取按钮权限Ids")
    public Result<List<Long>> queryPermissionIdsWithRoleId(@Parameter(description = "角色ID", required = true) @PathVariable("roleId") Long roleId) {
        return Result.success(sysRoleService.queryPermissionIdsWithRoleId(roleId));
    }
}
